const app = getApp()
let qCloud = require("../../index.js");

Page({
  /** 页面的初始数据 */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    warehouseId: 0, // 仓库ID
    warehouseName: "", // 仓库名称
    warehouseList: [], // 仓库列表
    goodsId: null, // 货物ID
    goodsName: "", // 货物名称(货号)
  },
  // 所属仓库
  warehouseChange(e) {
    let self = this;
    let index = e.detail.value;
    let warehouseList = self.data.warehouseList;
    self.setData({
      warehouseId: warehouseList[index].id, // 仓库ID
      warehouseName: warehouseList[index].title, // 仓库名称
    });
  },
  /** 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    let self = this;
    self.setData({
      goodsId: options.goodsId
    });

    // 查询仓储货物详情
    self.queryGoodsDetails();
  },
  /** 查询仓储货物详情 */
  queryGoodsDetails: function () {
    let self = this

    let params = {
      goodsId: self.data.goodsId
    }

    app.fetchApis(self, qCloud.api.warehouse_queryGoodsDetails, params, "POST", function (e) {
      let result = e.data;

      if (result.code == 200) {
        let data = result.data;

        self.setData({
          goodsName: data.goodsName, // 货物名称(货号)
          warehouseId: data.warehouseId, // 仓库ID
          warehouseName: data.warehouseName, // 仓库名称
        });

        // 查询仓库列表
        self.queryWarehouseList();
      }
    });
  },
  /** 查询仓库列表 */
  queryWarehouseList: function () {
    let self = this;

    let params = {};
    if (self.data.warehouseId == 0) {
      wx.showToast({
          title: "请选择仓库",
          icon: "error"
      });
      return;
    }

    app.fetchApis(self, qCloud.api.common_queryWarehouseList, params, "POST", function (e) {
      let result = e.data;

      if (result.code == 200) {
        let data = result.data;

        let warehouseName = "";
        let warehouseList = [];
        for (let i = 0; i < data.warehouseList.length; i++) {
          let warehouseInfo = data.warehouseList[i];
          for (let i = 0; i < warehouseInfo.subWarehouseList.length; i++) {
            let subWarehouseInfo = warehouseInfo.subWarehouseList[i];
            let warehouse = {
              id: subWarehouseInfo.warehouseId,
              title: warehouseInfo.warehouseName + " : " + subWarehouseInfo.warehouseName,
            };
            warehouseList.push(warehouse);

            if (self.data.warehouseId == subWarehouseInfo.warehouseId) {
              warehouseName = warehouseInfo.warehouseName + " : " + subWarehouseInfo.warehouseName;
            }
          }
        }

        self.setData({
          warehouseName: warehouseName,
          warehouseList: warehouseList
        });
      }
    });
  },
  // 调仓
  submitWarehouseAdjustment() {
    let self = this;

    let params = {
      warehouseId: self.data.warehouseId, // 仓库ID
      goodsId: self.data.goodsId, // 货物ID
    }

    app.fetchApis(self, qCloud.api.warehouse_submitWarehouseAdjustment, params, "POST", function (e) {
      let result = e.data;

      if (result.code == 200) {
        wx.navigateTo({
          url: "/pages/warehouse/index",
        });
      }
    });
  },
  /** 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {},
  /** 生命周期函数--监听页面显示 */
  onShow: function () {},
  /** 生命周期函数--监听页面隐藏 */
  onHide: function () {},
  /** 生命周期函数--监听页面卸载 */
  onUnload: function () {},
  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {},
  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {},
  /** 用户点击右上角分享 */
  onShareAppMessage: function () {}
});
